<?php

namespace app\admin\controller;

use app\common\controller\Commoncontroller;
use think\Request;
use think\Db;
use app\common\model\Base;


class Login extends Commoncontroller
{
    //登录页面
    public function index()
    {
        $request = Request::instance();
        if ($request->isPost()) {
            $request_data = $request->param();

            //验证密码是否正确
            $where['admin_name'] = $request_data['username'];
            $where['admin_password'] = MD5($request_data['password']);
            $where['is_deleted'] = 1;
            $result = db::name('admin')->field('admin_id,admin_name,roleid,avatar')->where($where)->find();
            if($result){

                //添加session,保存数据
                session('adminuid',$result['admin_id']);
                session('roleid',$result['roleid']);
                session('admin_name',$result['admin_name']);
                session('avatar',$result['avatar']);
                $arr_content['string'] = $result['admin_name'].'登录';
                $arr_content['opera_type'] = 5;
                $model = new Base();
                $model->add_admin_log($result['admin_id'],$arr_content,$result['admin_id'],'admin','用户');
                return $this->success('登录成功',url('index/index'));
            }else{
                return $this->error('用户名或者密码错误',url('login/index'));
            }

        }

       return  $this->fetch();
    }


    //退出登录
    public function logout(){
        $userId = session('adminuid');
        $adminName = session('admin_name');
        session('adminuid',null);
        session('roleid',null);
        $arr_content['string'] = $adminName.'退出登录';
        $arr_content['opera_type'] = 5;
        $model = new Base();
        $model->add_admin_log($userId,$arr_content,$userId,'admin','用户',0);
        return $this->success('退出成功',url('login/index'));
    }
}
